package cn.weakup.designpattern.a_foreword.calculator02;

/**
 * @author lixj
 * @version 1.0.0
 * @date 2022/6/2
 * @desc 将计算逻辑抽离为单独的类
 */
public class Operation {
    /**
     * 处理计算逻辑
     * @param numberA
     * @param numberB
     * @param option
     * @return
     */
    public static String handleProgram(String numberA, String numberB, String option) {
        Double result;
        try {
            Double numA = Double.valueOf(numberA);
            Double numB = Double.valueOf(numberB);
            switch (option) {
                case "+":
                    result = numA + numB;
                    break;
                case "-":
                    result = numA - numB;
                    break;
                case "*":
                    result = numA * numB;
                    break;
                case "/":
                    result = numA / numB;
                    break;
                default:
                    throw new IllegalStateException("Unexpected value: " + option);
            }
        } catch (Exception e) {
            return e.getMessage();
        }
        return result.toString();
    }
}
